﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Services;
using CoolCode.Entities;
using CoolCode.Web.UI;

namespace CoolCode.Web.UI.Views.Check
{
    public partial class ProductCriterions : AuthPage
    {
        private ICriterionService _Service;

        public ICriterionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = new CriterionService(Config.AppData , this.Cache );
                }
                return _Service;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            txtProductSpec.ReadOnly = true;
            pnlEntry.Visible = false;
            if (!IsPostBack)
            {
                BindData_Master();
            }
        }

        private void handleEvents()
        {
            gvMaster.RowEditing += new GridViewEditEventHandler(gvMaster_RowEditing);
            btnSave.Click += new EventHandler(btnSave_Click);
        }        

        private void BindData_Master()
        {
            var list = this.Service.List();
            gvMaster.DataSource = list;
            gvMaster.DataBind();
        }

        protected void gvMaster_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string key = gvMaster.GetKey<string>(e.NewEditIndex);
            ProductCriterion entity = this.Service.Get(key);
            txtProductSpec.Text = entity.ProductSpec;
            rngODFar.TextFrom = entity.MinODFar.ToString();
            rngODFar.TextTo = entity.MaxODFar.ToString();
            rngODClose.TextFrom = entity.MinODClose.ToString();
            rngODClose.TextTo = entity.MaxODClose.ToString();
            rngConeHeight.TextFrom = entity.MinConeHeight.ToString();
            rngConeHeight.TextTo = entity.MaxConeHeight.ToString();
            rngSacculusLength.TextFrom = entity.MinSacculusLength.ToString();
            rngSacculusLength.TextTo = entity.MaxSacculusLength.ToString();
            rngSacculusStretch.TextFrom = entity.MinSacculusStretch.ToString();
            rngSacculusStretch.TextTo = entity.MaxSacculusStretch.ToString();
            rngStressTest2.TextFrom = entity.MinODStressTest2.ToString();
            rngStressTest2.TextTo = entity.MaxODStressTest2.ToString();
            rngStressTestLength.TextFrom = entity.MinStressTestLength.ToString();
            rngStressTestLength.TextTo = entity.MaxStressTestLength.ToString();
            pnlEntry.Visible = true;
            udpEntry.Update();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ProductCriterion entity = new ProductCriterion
            {
                ProductSpec = txtProductSpec.Text,
                MinODFar = rngODFar.TextFrom.To<decimal>(),
                MaxODFar = rngODFar.TextTo.To<decimal>(),
                MinODClose = rngODClose.TextFrom.To<decimal>(),
                MaxODClose = rngODClose.TextTo.To<decimal>(),
                MinConeHeight = rngConeHeight.TextFrom.To<decimal>(),
                MaxConeHeight = rngConeHeight.TextTo.To<decimal>(),
                MinSacculusLength = rngSacculusLength.TextFrom.To<decimal>(),
                MaxSacculusLength = rngSacculusLength.TextTo.To<decimal>(),
                MinSacculusStretch = rngSacculusStretch.TextFrom.To<decimal>(),
                MaxSacculusStretch = rngSacculusStretch.TextTo.To<decimal>(),
                MinODStressTest2 = rngStressTest2.TextFrom.To<decimal>(),
                MaxODStressTest2 = rngStressTest2.TextTo.To<decimal>(),
                MinStressTestLength = rngStressTestLength.TextFrom.To<decimal>(),
                MaxStressTestLength = rngStressTestLength.TextTo.To<decimal>(),
            };
            this.Service.Save(entity);
            BindData_Master();
            udpHeader.Update();
            udpEntry.Alert("保存成功！");
        }
    }
}
